package cn.copera.dyy.common.utils.filter.builder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class BuildFilterBean implements FieldContainer {
	public static HashMap<String, String> relationals = new HashMap<String, String>();

	public static final String NE = "ne";

	public static final String GT = "gt";

	public static final String LT = "lt";

	public static final String GE = "ge";

	public static final String LE = "le";

	public static final String LK = "lk";

	public static final String EQ = "eq";

	public static final String CHAR = "char";

	public static final String NUMBER = "number";

	public static final String DATE = "date";

	public static final String AND = "AND";

	public static final String OR = "OR";

	public static final String IS = "is";
	static {
		relationals.put("ne", "<>");
		relationals.put("gt", ">");
		relationals.put("lt", "<");
		relationals.put("ge", ">=");
		relationals.put("le", "<=");
		relationals.put("lk", " like ");
		relationals.put("eq", "=");
		relationals.put("is", "is");
		relationals.put("in", " in ");
	}

	private List<Field> fields = new ArrayList<Field>();

	private String filter, newQuery;

	public void setFilter(String filter) {
		this.filter = filter;
	}
	public String getFilter() {

		if (fields.size() > 0) {
			StringBuffer th = new StringBuffer();
			boolean first = true;
			for (Field f : fields) {
				String singleSql = f.getSql();

				if (singleSql == null || "".equals(singleSql))
					continue;

				if (first) {
					th.append(singleSql);
					first = false;
				} else {
					String logical = f.getLogical();

					logical = logical == null || "".equals(logical.trim()) ? "AND"
							: logical;
					
					th.append(' ').append(logical).append(' ')
							.append(singleSql);
				}

				filter = th.toString();
			}
		} else
			filter = "";
	
		return filter;
	}


	

	public String getNewQuery() {
		return newQuery;
	}

	public void setNewQuery(String newQuery) {
		this.newQuery = newQuery;
	}

	public boolean isNew() {
		return newQuery != null ? true : filter == null
				|| "".equals(filter.trim());
	}

	public void add(Field field) {
		fields.add(field);
	}
}